package com.share.zabbix.method;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.share.zabbix.request.ZabbixRequest;
import com.share.zabbix.request.ZabbixRequestBuilder;
import com.share.zabbix.response.ZabbixResponseHistory;

import cn.hutool.core.util.StrUtil;

public class ZabbixHistoryMethod extends ZabbixBaseMethod{


	public static List<ZabbixResponseHistory> getHistory(String itemId, Date begin, Date end, Integer limit) {
		ZabbixRequest request = ZabbixRequestBuilder.newBuilder()
				.method("history.get")
				.extendOutput()
				.paramEntry("history", 0)
				.sortByDesc("clock")
				.limitGet(limit == null ? Integer.MAX_VALUE : limit)
				.build();
		if (!StrUtil.isEmpty(itemId))
			request.putParam("itemids", itemId);
		if (begin != null)
			request.putParam("time_from", begin.getTime() / 1000);
		if (end != null)
			request.putParam("time_till", end.getTime() / 1000);
		try {
			JSONObject response = call(request);
			Gson gson = new GsonBuilder().create();
			List<ZabbixResponseHistory> hisls = gson.fromJson(response.getJSONArray(ZABBIX_KEY_RESULT).toString(),
					new TypeToken<ArrayList<ZabbixResponseHistory>>() {
					}.getType());

			return hisls;
		} catch (Exception e) {
			log.error(e);
			log.info("Zabbix查询history失败！");
		}
		return null;
	}
}
